﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;
using Entities.Admin;
using BLL.PBKT;



namespace WindowsForms.PBKT
{
	public partial class FormThemTTLichThiDau : Form
	{
		public bool trangThai = false;
		public LichThiDau ltDau = null;

		private QLTranDau qlTranDau = new QLTranDau();
		private ChiTietTranDau ctTranDau = new ChiTietTranDau();
		List<TrongTai> dsTT;
		List<DoiTuyen> dsDT;
		List<CauThu> dsctDT;
		//List<CauThu> dsctDT2;

		public FormThemTTLichThiDau(LichThiDau ldt)
		{
			ltDau = ldt;
			InitializeComponent();
			dgvDSTrongTai.AutoGenerateColumns = false;

			dsTT = TrongTai.Objects.All().ToList();
			dsDT = DoiTuyen.Objects.All().ToList();
			LoadComboxDoiTuyen(cbbMD1, dsDT);
			LoadDGVTrongTai(dsTT);

			LoadComboxDoiTuyen(cbbMD2, DoiTuyen.Objects.All().ToList());



		}
		private void LoadFormUpdate(bool sate, LichThiDau ltd)
		{
			if (sate && ltd != null)
			{
				mtbMaLichTD.Text = ltd.MaLichThiDau;
				dtpThoiGian.Value = ltd.ThoiGian;
				cbbTT.SelectedIndex = ltd.TrangThai;
				txtTySo.Text = ltd.TySo;
				for (int i = 0; i < cbbDiaDiem.Items.Count; i++)
					if (cbbDiaDiem.Items[i].ToString() == ltd.DiaDiem)
						cbbDiaDiem.SelectedIndex = i;

				for (int i = 1; i < cbbMD1.Items.Count; i++)
				{

					DoiTuyen dt = cbbMD1.Items[i] as DoiTuyen;

					if (dt.MaDT.CompareTo(ltd.MaDoi1) == 0)
						cbbMD1.SelectedIndex = i;
					dt = cbbMD2.Items[i] as DoiTuyen;
					if (dt.MaDT.CompareTo(ltd.MaDoi2) == 0)
						cbbMD2.SelectedIndex = i;
				}
				for (int i = 1; i < cbbMaGiaiDau.Items.Count; i++)
				{

					GiaiDau gd = cbbMaGiaiDau.Items[i] as GiaiDau;

					if (gd.MaGiaiDau == ltd.MaGD)
						cbbMaGiaiDau.SelectedIndex = i;

				}
				List<String> dsMaTT = ScheduleManager.GetDSMaTrongTai(ltd);

				for (int i = 0; i < dgvDSTrongTai.RowCount; i++)
				{
					string mTT = dgvDSTrongTai.Rows[i].Cells[1].Value.ToString();

					//ChiTietTranDau ctTd = ChiTietTranDau.Objects.Find(x => x.MaLichThiDau == ltd.MaLichThiDau && x.MaNguoiTG == "TT_005").Single<ChiTietTranDau>();

					if (dsMaTT.Contains(mTT))
					{
						qlTranDau = QLTranDau.Objects.Find(x => x.MaLichThiDau == ltd.MaLichThiDau && x.MaNguoiTG == mTT).Single<QLTranDau>();
						if (qlTranDau != null)
						{
							DataGridViewCheckBoxCell chb = dgvDSTrongTai.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
							chb.Value = true;
							DataGridViewComboBoxCell cbbCell = dgvDSTrongTai.Rows[i].Cells[4] as DataGridViewComboBoxCell;
							//int HinhThuc = ctTd.HinhThuc;
							//String hinhThuc = "Bắt chính";
							cbbCell.Value = qlTranDau.HinhThuc == 1 ? "Bắt chính" : "Bắt phụ";

							//MessageBox.Show(cbbCell.Value.ToString());

						}
					}

					//MessageBox.Show(cbbCell.FormattedValue.ToString());
					LoadDSCauThu(ltd);
				}
			}
		}
		private void LoadDSCauThu(LichThiDau ltd)
		{
			List<String> dsMaCT = ScheduleManager.GetDSMaCuaThu(ltd);
			List<ChiTietTranDau> listCT = ScheduleManager.GetListCTTD(ltd, dsMaCT);
			dgvDSCauThu.DataSource = listCT;
			if (dgvDSCauThu.DataSource != null)
				for (int i = 0; i < dgvDSCauThu.RowCount; i++)
				{
					CauThu ct = CauThu.Objects.GetById(dgvDSCauThu.Rows[i].Cells[0].Value);
					dgvDSCauThu.Rows[i].Cells[1].Value = ct.TenCauThu;
					dgvDSCauThu.Rows[i].Cells[2].Value = ct.MaDT;
					dgvDSCauThu.Rows[i].Cells[3].Value = ct.SoAo;
					string s;
					if (listCT[i].HinhThuc == 1)
						s = "Ghi bàn";
					else if (listCT[i].HinhThuc == 2)
						s = "Thẻ đỏ";
					else s = "Thẻ vàng";
					DataGridViewComboBoxCell cbbCell = dgvDSCauThu.Rows[i].Cells[4] as DataGridViewComboBoxCell;
					cbbCell.Value = s;
				}
		}
		private void LoadComboxDoiTuyen(ComboBox cbb, List<DoiTuyen> list)
		{
			if (list != null)
			{

				cbb.DisplayMember = "TenDT";
				cbb.ValueMember = "MaDT";
				foreach (var item in list)
					cbb.Items.Add(item);
				cbb.Items.Insert(0, "Chọn đội tuyển");

				cbb.SelectedIndex = 0;

			}
		}
		private void LoadDGVTrongTai(List<TrongTai> list)
		{
			dgvDSTrongTai.DataSource = list;
			int i = 0;

			for (i = 0; i < dgvDSTrongTai.RowCount; i++)
			{
				TrongTai tt = TrongTai.Objects.GetById(dgvDSTrongTai.Rows[i].Cells[1].Value);

				DiaChi dc = DiaChi.Objects.GetById(tt.MaDiaChi);
				//	dgvDSTrongTai.Rows[i].Cells[3].Value = dc.ToString();
				//dgvDSTrongTai.Rows[i].Cells[3].Value = "jgj";
			}



		}
		private void AddDiaChi(DiaChi dc)
		{
			string s_Dc = dc.SoNha + " " + dc.TenDuong + " " + dc.QuanHuyen + " " + dc.TinhTP;
			for (int i = 0; i < dsTT.Count - 1; i++)
			{
				dgvDSTrongTai[i, 3].Value = s_Dc;
			}
		}
		private LichThiDau GetLichThiDau()
		{
			LichThiDau ltd = null;
			try
			{
				ltd = new LichThiDau();
				ltd.MaLichThiDau = mtbMaLichTD.Text;
				ltd.ThoiGian = dtpThoiGian.Value;
				ltd.TrangThai = cbbTT.SelectedIndex;
				DoiTuyen doi1 = cbbMD1.SelectedItem as DoiTuyen;
				ltd.MaDoi1 = doi1.MaDT;
				doi1 = cbbMD2.SelectedItem as DoiTuyen;
				ltd.MaDoi2 = doi1.MaDT;
				ltd.TySo = txtTySo.Text;
				GiaiDau giaiDau = cbbMaGiaiDau.SelectedItem as GiaiDau;
				ltd.MaGD = giaiDau.MaGiaiDau;
				ltd.DiaDiem = cbbDiaDiem.SelectedItem.ToString();

				return ltd;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message + "\nBạn xem lại trường Mã Lịch Thi Đấu \n\tĐội tuyển\n\tĐịa điểm\n\tTrạng thái");
				return null;
			}
		}

		private void btnLuu_Click(object sender, EventArgs e)
		{
			String s = "";
			int count = 0;
			LichThiDau ltd = GetLichThiDau();
			//DialogResult rs = new DialogResult();
			//if (trangThai)
			//    rs = MessageBox.Show("Bạn có muốn cập nhật không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			//else
			//if (rs == DialogResult.Yes)
			//{
			if (LichThiDau.Objects.InsertOrUpdate(ltd))
			{
				count++;
				for (int j = 0; j < dgvDSTrongTai.RowCount; j++)
				{
					if (dgvDSTrongTai.Rows[j].Cells[4].Value != null && dgvDSTrongTai.Rows[j].Cells[0].Value.ToString() == "True")
					{
						s = String.Format("{0}-{1}", dgvDSTrongTai.Rows[j].Cells[1].Value, dgvDSTrongTai.Rows[j].Cells[4].Value);
						Arbitration.AddTrongTai(ltd, s);
					}

				}
				if (dgvDSCauThu.DataSource != null)
					for (int i = 0; i < dgvDSCauThu.RowCount; i++)
					{
						if (dgvDSCauThu.Rows[i].Cells[4].Value != null && dgvDSCauThu.Rows[i].Cells[5].Value != null)
						{
							s = String.Format("{0}-{1}-{2}", dgvDSCauThu.Rows[i].Cells[0].Value, dgvDSCauThu.Rows[i].Cells[4].Value, dgvDSCauThu.Rows[i].Cells[5].Value);
							PlayerFootball.AddCauThu(ltd, s);
						}


					}
				if (count != 0)
					MessageBox.Show("Lưu thành công");
				else
				{
					if (!trangThai)
						LichThiDau.Objects.Delete(ltd);
					MessageBox.Show("Lưu không thành công");
				}

			}

			//}
			LoadDSCauThu(ltd);

		}



		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		private void FormThemTTLichThiDau_Load(object sender, EventArgs e)
		{
			trangThai = UCLichThiDau.state;
			grpDSCauThu.Enabled = trangThai;
			dgvDSCauThu.AutoGenerateColumns = false;
			dgvDSTrongTai.AutoGenerateColumns = false;
			cbbMaGiaiDau.DataSource = GiaiDau.Objects.All().ToList();
			cbbMaGiaiDau.ValueMember = "MaGiaiDau";
			cbbMaGiaiDau.DisplayMember = "TenGiaiDau";
			cbbDiaDiem.SelectedIndex = cbbMaGiaiDau.SelectedIndex = cbbMD1.SelectedIndex = cbbMD2.SelectedIndex = cbbDiaDiem.SelectedIndex = 0;
			if (trangThai)
			{
				LoadFormUpdate(trangThai, ltDau);
				//dsctDT = CauThu.Objects.Find(x => x.MaDT == ltDau.MaDoi1).ToList();
				//dsctDT2 = CauThu.Objects.Find(x => x.MaDT == ltDau.MaDoi2).ToList();
				//dsctDT.InsertRange(dsctDT.Count, dsctDT2);
				//dgvDSCauThu.DataSource = dsctDT;
			}
		}

		private void btnThemCT_Click(object sender, EventArgs e)
		{
			ltDau = null;
			ltDau = LichThiDau.Objects.GetById(mtbMaLichTD.Text);
			if (ltDau != null)
			{
				FormTimKiemCT frm = new FormTimKiemCT(ltDau);
				frm.ShowDialog();
				dsctDT = frm.outDSTT;
				if (dsctDT != null)
					dgvDSCauThu.DataSource = dsctDT;
				//	foreach (var item in dsctDT)
				//	{
				//DataGridViewRow row = (DataGridViewRow)dgvDSCauThu.Rows[0].Clone();
				//row.Cells[0].Value = item.MaCT;
				//row.Cells[1].Value = item.TenCauThu;
				//row.Cells[2].Value = item.MaDT;
				//row.Cells[3].Value = item.SoAo;

				//dgvDSCauThu.Rows.Add(row);
				//dgvDSCauThu.Rows.Add(item.MaDT, item.TenCauThu, item.MaDT, item.SoAo);

				//	}

				//dgvRow.Cells[0].Value = dsctDT[0].MaCT;
				//dgvDSCauThu.Rows.AddRange(

			}
			else
				MessageBox.Show("Lịch thi đâu này chưa tồn tại trong cơ sơ dữ liệu\n Bạn có thể Click vào Lưu để thêm mới");

		}

		private void dgvDSTrongTai_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 5)
				{
					if (dgvDSTrongTai.Rows[e.RowIndex].Cells[0].Value.ToString() == "True")
					{
						DataGridViewCheckBoxCell chb = dgvDSTrongTai.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
						chb.Value = false;
						DataGridViewComboBoxCell cbb = dgvDSTrongTai.Rows[e.RowIndex].Cells[4] as DataGridViewComboBoxCell;
						cbb.Value = "";
						string mtt = dgvDSTrongTai.Rows[e.RowIndex].Cells[1].Value.ToString() + "";
						QLTranDau qltd = QLTranDau.Objects.Find(c => c.MaLichThiDau == ltDau.MaLichThiDau && c.MaNguoiTG.CompareTo(mtt) == 0).Single();
						QLTranDau.Objects.Delete(qltd);
					}

				}
				if (e.ColumnIndex == 6)
				{
					TrongTai tt = TrongTai.Objects.GetById(dgvDSTrongTai.Rows[e.RowIndex].Cells[1].Value);
					FormTrongTaiThem frm = new FormTrongTaiThem(tt);
					frm.ShowDialog();
				}
			}
			catch (Exception ex)
			{

				MessageBox.Show(ex.Message);
			}


		}

		private void dgvDSCauThu_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 6)
				{
					CauThu cauThu = CauThu.Objects.GetById(dgvDSCauThu.Rows[e.RowIndex].Cells[0].Value);
					LichThiDau ltd = LichThiDau.Objects.GetById(mtbMaLichTD.Text);
					ChiTietTranDau chiTietTD = ChiTietTranDau.Objects.Find(c => c.MaLichThiDau == ltd.MaLichThiDau && c.MaCT == cauThu.MaCT).Single();
					if (ltd != null && cauThu != null)
					{
						if (ChiTietTranDau.Objects.Delete(chiTietTD))
						{
							MessageBox.Show("Xóa thành công");
							LoadDSCauThu(ltd);
						}
						else
							MessageBox.Show("Xóa không thành công");
					}
				}
			}
			catch (Exception ex)
			{

				MessageBox.Show(ex.Message);

			}
		}

	}
}
